<?php

namespace App\Http\Controllers\Admin;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Repos\ProductRepo;
use App\Repos\ShopRepo;
use App\Repos\SpecRepo;
use App\Repos\OrderPointRepo;
use App\Models\Cate;


class ProductController extends Controller
{
    public function index(Request $request)
    {
    	$where = [];
    	$data = $request->except(['token','_method','page']);
        $where = array_where($data, function ($value, $key) {
            return $where[$key] = $value;
        });
        //dd($where);
    	$list = ProductRepo::getListPages($where);
    	$cates = Cate::select(['id','name'])->get();

       
    	return view('admin.product.index',compact('list','where','cates'));
    }

    public function add(Request $request)
    {
        $cates = Cate::select(['id','name'])->get();       
        return view('admin.product.add',compact('cates')); 
    }

    public function save(Request $request)
    {
        $rs['status'] = 'danger';
        $data = $request->all();
        if ($request->file('home_thumb')) {
            $data['home_thumb'] = '/'.$request->home_thumb->store('uploads/'.date('Ymd'));     
        }
        //dd($data);
        $pics = [];
        if($request->pics){
            $pics = rtrim($request->pics,',');
            $pics = explode(',',$pics);
            $data['thumb'] = $pics[0];
        }
        unset($data['pics']);
        $flag = ProductRepo::create($data,$pics);
        if($flag['status']==true) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return redirect('zadmin/products')->with('rs',$rs);
        }
    }

    public function update(Request $request)
    {
        $rs['status'] = 'danger';
        $data = $request->except(['_token','id']);
        $id = $request->id;

        if ($request->file('home_thumb')) {
            $data['home_thumb'] = '/'.$request->home_thumb->store('uploads/'.date('Ymd'));     
        }
        
        $pics = [];
        if($request->pics){
            $pics = rtrim($request->pics,',');
            $pics = explode(',',$pics);
            $data['thumb'] = $pics[0];
        }
        unset($data['pics']);

        //dd($data);
        $flag = ProductRepo::update($id,$data,$pics);
        if($flag['status']==true) {
            $rs['status'] = 'success';
            $rs['msg'] = '操作成功';
            return redirect('zadmin/products')->with('rs',$rs);
        } 

        $rs['msg'] = $flag['msg'];
        return redirect('zadmin/products')->with('rs',$rs);
    }

    public function edit(Request $request)
    {   
        $id = $request->id;
        $cates = Cate::select(['id','name'])->get(); 
        $where['id'] = $id; 
        $data  = ProductRepo::find($where); 
        $specs = ProductRepo::getSpecs($id);     
        return view('admin.product.edit',compact('cates','data','specs'));
    }

    public function setThumb(Request $request)
    {
        $rs['status'] = 'success';
        $rs['msg']    = '操作成功';
        $id    = $request->product_id;
       
        $flag = ProductRepo::setThumb($id,$request->pic);
        if($flag) {

            return redirect('zadmin/products')->with('rs',$rs);
        }

        return back()->with('rs',$rs);
    }

    

    public function saleStatus(Request $request)
    {
        $id = $request->id;
        $data = $request->except(['_token']);
        $data['admin'] =1;
        $flag = ProductRepo::ChangeStatus($id,$data);
        return back();
    }

    /**
     * 修改推荐的状态
     */
    public function changeRec(Request $request)
    {
        $id = $request->id;
        $data['is_rec'] = $request->rec;
        
        $flag = ProductRepo::update($id,$data);
        
        return back();
    }

    public function destroy($id)
    {
        ProductRepo::destroy($id);
        return back();
    }


    public function spec(Request $request)
    {
        $product_id = (int)$request->product_id;
        $product    = ProductRepo::find(['id'=>$product_id]);
        $where['product_type_id'] = $product->type_id;
        $specs      = SpecRepo::getList($where); 
        $productSpecs = ProductRepo::getProductSpecs($product_id);
        return view('admin.product.spec',compact('product','specs','productSpecs'));
    }

    public function specSave(Request $request)
    {   
        $rs['status'] = 'success';
        $rs['msg']    = '操作成功';
        $data = $request->except(['_token']);
        $flag = ProductRepo::saveSpec($data);
        return back()->with('rs',$rs);
    }

    public function imgDel(Request $request)
    {
        $id = $request->id;
        ProductRepo::imgDel($id);
         $rs['status'] = 'success';
        $rs['msg']    = '操作成功';
        return back()->with('rs',$rs);
    }


}
